package ch.il07.converter.persistance.tests;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import ch.il07.converter.persistence.Persistence;
import ch.il07.converter.persistence.PersistenceHelper;
import ch.il07.converter.persistence.entities.AccessLevel;
import ch.il07.converter.persistence.entities.Measurand;
import ch.il07.converter.persistence.entities.Unit;
import ch.il07.converter.persistence.exceptions.AlreadyDefinedException;
import ch.il07.converter.persistence.exceptions.PersistenceException;
import ch.il07.utils.RandomHelper;

public class AddMeasurandTest {

	private Persistence persistence;

	@Before
	public void setUp() {
		try {
			persistence = PersistenceHelper.getPersistence();
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}
	
	@Test
	public void normalPublicAdd() {

		try {
			for (int i = 0; i < 3; i++) {
				persistence.addMeasurand(new Measurand(RandomHelper.randString(), new Unit(RandomHelper.randString(), 1D)),
						null);
			}
		} catch (AlreadyDefinedException e) {
			fail(e.getMessage());
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}
	
	@Test
	public void checkAttributes(){
		Measurand m = new Measurand(RandomHelper.randString(), new Unit(RandomHelper.randString(), 1D));
		m.setDescription(RandomHelper.randString());
		String username = RandomHelper.randString();
		
		try {
			persistence.addUser(username, RandomHelper.getEmail(username), username, AccessLevel.USER);
			assertNotNull("user '"+username+"' should exist in db", persistence.getUser(username, username));
			
			persistence.addMeasurand(m, username);
			
			List<Measurand> measurands = persistence.getMeasurands(username);
			assertTrue("user '"+username+"' should have 1 measurand", measurands.size() == 1);
			
			assertNotNull("description should be set for measurand '"+m.getName()+"'", measurands.get(0).getDescription());
		} catch (AlreadyDefinedException e) {
			fail(e.getMessage());
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void normalPrivateAdd() {
		try {
			for (int i = 0; i < 3; i++) {
				persistence.addMeasurand(new Measurand(RandomHelper.randString(), new Unit(RandomHelper.randString(), 1D)),
						"admin");
			}
		} catch (AlreadyDefinedException e) {
			fail(e.getMessage());
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void addTwicePrivate() {
		Measurand m = new Measurand(RandomHelper.randString(), new Unit(RandomHelper.randString(), 1D));
		String user = "admin";

		try {
			persistence.addMeasurand(m,
					user);
			persistence.addMeasurand(m,
					user);
			fail("Should not be possible to add twice the same measurand");
		} catch (AlreadyDefinedException e) {
			// expected
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}
	
	@Test
	public void addTwicePublic() {
		Measurand m = new Measurand(RandomHelper.randString(), new Unit(RandomHelper.randString(), 1D));

		try {
			persistence.addMeasurand(m,
					null);
			persistence.addMeasurand(m,
					null);
			fail("Should not be possible to add twice the same measurand");
		} catch (AlreadyDefinedException e) {
			// expected
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}
}
